﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DbView
{
	public partial class SelectDriverForm : Form
	{
		public SelectDriverForm(Model model)
		{
			InitializeComponent();
			this.model = model;
			LoadList();
		}

		private void LoadList()
		{
			string PluginInterface = "DbViewDriver.IDataSource";
			foreach (object plugin in PluginManager.Instance.GetPlugins(PluginInterface))
			{
				DbViewDriver.IDataSource iplug = plugin as DbViewDriver.IDataSource;
				this.listDrivers.Items.Add(iplug.DriverName);
			}
			this.listDrivers.SelectedItem = this.model.DataSource.DriverName;
		}

		Model model;

		private void btnOk_Click(object sender, EventArgs e)
		{
			OnSelect();
		}

		private void OnSelect()
		{
			this.DialogResult = DialogResult.OK;
			string selected = this.listDrivers.SelectedItem.ToString();
			string PluginInterface = "DbViewDriver.IDataSource";
			foreach (object plugin in PluginManager.Instance.GetPlugins(PluginInterface))
			{
				DbViewDriver.IDataSource iplug = plugin as DbViewDriver.IDataSource;
				if (iplug.DriverName == selected)
					this.model.SetDriver(iplug);
			}
			this.DialogResult = DialogResult.OK;
		}

		private void listDrivers_DoubleClick(object sender, EventArgs e)
		{
			OnSelect();
			this.Close();
		}


	}
}
